package org.ghost.wechat.platform.util;

import org.springframework.util.Assert;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Administrator
 */
public class MapResource<K, V> {
    private Map<K, V> map;

    public MapResource() {
        this(false);
    }

    public MapResource(boolean linkedHashMap) {
        if (linkedHashMap) {
            this.map = new LinkedHashMap<K, V>();
        } else {
            this.map = new HashMap<K, V>();
        }
    }

    public MapResource(K key, V value) {
        this(false);
        this.add(key, value);
    }

    public MapResource(boolean linkedHashMap, K key, V value) {
        this(linkedHashMap);
        this.add(key, value);
    }

    public MapResource<K, V> add(K key, V value) {
        Assert.notNull(key, "key must not be null");
        this.map.put(key, value);
        return this;
    }

    public Map<K, V> toMap() {
        return Collections.unmodifiableMap(this.map);
    }
}